//
//  ZZnetworking.m
//  iosYunyue
//
//  Created by mac on 16/4/8.
//  Copyright © 2016年 zhanlijun. All rights reserved.
//

#import "ZZnetworking.h"
#import "AFNetworking.h"


@implementation ZZnetworking

#pragma mark Get获取数据

+ (void)getdataWithUrl:(NSString *)urlStr parameters:(id)parameters success:(void (^)(id responseObject))success fail:(void (^)())fail
{
    //初始化请求管理器
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    //设置接受类型
    manager.responseSerializer.acceptableContentTypes =[NSSet setWithObjects:@"application/json",@"text/html",@"text/plain", nil];
    //发送GET请求
    
    
    [manager GET:urlStr parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"aaa%@",error);
        if (fail) {
            fail();
        }
    }];
}

#pragma mark - JSON方式post提交数据
+ (void)postJSONWithUrl:(NSString *)urlStr parameters:(id)parameters success:(void (^)(id responseObject))success fail:(void (^)())fail
{
    
    //初始化请求管理器
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //设置接受类型
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/html", nil];
    //发送POST请求
    [manager POST:urlStr parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"删除失败 %@",error);
        if (fail) {
            fail();
        }
    }];
}
#pragma mark - JSON方式post提交数据和图片
+ (void)postJSONWithUrl:(NSString *)urlStr parameters:(id)parameters imagedata:(NSData *)imageData success:(void (^)(id responseObject))success fail:(void (^)())fail
{
    //初始化请求管理器
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //设置接受类型
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/html", nil];
    //发送POST请求
    [manager POST:urlStr parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        // 可以在上传时使用当前的系统事件作为文件名
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        // 设置时间格式
        formatter.dateFormat = @"yyyyMMddHHmmss";
        NSString *str = [formatter stringFromDate:[NSDate date]];
        NSString *fileName = [NSString stringWithFormat:@"%@.jpg", str];
        
        /*
         此方法参数
         1. 要上传的[二进制数据]
         2. 对应网站上[upload.php中]处理文件的[字段"file"]
         3. 要保存在服务器上的[文件名]multipart/form-data
         4. 上传文件的[mimeType]
         */
        if(imageData!=nil){
            
            [formData appendPartWithFileData:imageData name:@"photo" fileName:fileName mimeType:@"multipart/form-data"];
            
        }
        
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"上传成功 %@",responseObject);
        if (success) {
            success(responseObject);
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (fail) {
            fail();
        }
    }];
}


#pragma mark - JSON方式post提交数据和图片
+ (void)postJSONWithUrl:(NSString *)urlStr parameters:(id)parameters imagedata:(NSData *)imageData imagename:(NSString *)imagename success:(void (^)(id responseObject))success fail:(void (^)())fail
{
    //初始化请求管理器
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //设置接受类型
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/html", nil];
    //发送POST请求
    [manager POST:urlStr parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        // 可以在上传时使用当前的系统事件作为文件名
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        // 设置时间格式
        formatter.dateFormat = @"yyyyMMddHHmmss";
        NSString *str = [formatter stringFromDate:[NSDate date]];
        NSString *fileName = [NSString stringWithFormat:@"%@.jpg", str];
        
        /*
         此方法参数
         1. 要上传的[二进制数据]
         2. 对应网站上[upload.php中]处理文件的[字段"file"]
         3. 要保存在服务器上的[文件名]multipart/form-data
         4. 上传文件的[mimeType]
         */
        if(imageData!=nil){
            
            [formData appendPartWithFileData:imageData name:imagename fileName:fileName mimeType:@"multipart/form-data"];
            
        }
        
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"上传成功 %@",responseObject);
        if (success) {
            success(responseObject);
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (fail) {
            fail();
        }
    }];
}
#pragma mark 上传多张图片
+ (void)postJSONWithUrl:(NSString *)urlStr parameters:(id)parameters imageArray:(NSArray *)imageArray success:(void (^)(id responseObject))success fail:(void (^)())fail
{
    //初始化请求管理器
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //设置接受类型
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/html", nil];
    //发送POST请求
    [manager POST:urlStr parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        
        int i = 0;
        for (UIImage *image in imageArray) {
            
            // 可以在上传时使用当前的系统事件作为文件名
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            // 设置时间格式
            formatter.dateFormat = @"yyyyMMddHHmmss";
            NSString *str = [formatter stringFromDate:[NSDate date]];
            NSString *fileName = [NSString stringWithFormat:@"%@.jpg", str];
            
            NSData *imageData = UIImageJPEGRepresentation(image, 0.6f);
            /*
             此方法参数
             1. 要上传的[二进制数据]
             2. 对应网站上[upload.php中]处理文件的[字段"file"]
             3. 要保存在服务器上的[文件名]multipart/form-data
             4. 上传文件的[mimeType]
             */
            if(imageData!=nil){
                NSString *name = [NSString stringWithFormat:@"photo%d",i];
                [formData appendPartWithFileData:imageData name:name fileName:fileName mimeType:@"multipart/form-data"];
                
            }
            i++;
        }
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"上传成功 %@",responseObject);
        if (success) {
            success(responseObject);
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (fail) {
            fail();
        }
    }];
    
    
}
@end
